package me.passtime.projt.query;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.*;
import me.passtime.projt.Home;
import me.passtime.projt.R;
import me.passtime.projt.util.*;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-6
 * Time: 下午3:41
 */
public class QueryHistoryPage extends Activity implements View.OnClickListener {
    private Button mUserInfo;
    private TextView mPhoneNumber;
    private TextView mFeeType;
    private TextView mFeeName;
    private SimpleAdapter listAdapter;
    private ListView mListView;
    private List<HashMap<String, String>> listDate = new ArrayList<HashMap<String, String>>();

    private List<String> selectData = new ArrayList<String>();
    private Spinner spinner;
    private ArrayAdapter<String> adapter;
    private Calendar calendar = Calendar.getInstance();

    private Handler mHandler;

    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 9;
    private QueryController mController;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_query);
        mController=new QueryController(this);
        mHandler = new Handler();
        initData();
        this.findViewById(R.id.query_history_back_btn).setOnClickListener(this);
        this.findViewById(R.id.query_history_logout_btn).setOnClickListener(this);
        this.findViewById(R.id.query_history_my_account_btn).setOnClickListener(this);
        this.findViewById(R.id.query_history_fee_records_btn).setOnClickListener(this);
        this.findViewById(R.id.query_history_exit).setOnClickListener(this);
    }

    private void initData() {
        mUserInfo = (Button) this.findViewById(R.id.query_history_person_btn);
        mUserInfo.setText(UserHolder.phoneNumber + "\r\n" + UserHolder.userName);
        mPhoneNumber = (TextView) this.findViewById(R.id.query_history_phone_number);
        mPhoneNumber.setText(UserHolder.phoneNumber);
        mFeeType = (TextView) this.findViewById(R.id.query_history_fee_type);

        mFeeName = (TextView) this.findViewById(R.id.query_history_fee_name);


        mListView = (ListView) this.findViewById(R.id.query_history_list_view);
        listAdapter = new SimpleAdapter(this, listDate, R.layout.month_list_view_item,
                new String[]{"itemTitle", "itemText"},
                new int[]{R.id.item_name, R.id.item_value});
        mListView.setAdapter(listAdapter);
        queryHistoryList(new Date());
        initSpinnerData();
        spinner = (Spinner) findViewById(R.id.query_history_dropdown);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, selectData);
        adapter.setDropDownViewResource(android.R.layout.select_dialog_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
    }

    private void queryHistoryList(Date date) {
        showDialog(DIALOG_PROGRESS);
        mController.queryHT(UserHolder.phoneNumber,DateUtils.formatDateToQueryYearMonth(date));
    }
    public void updateListDate(String packageName,List<String> records){
        mFeeType.setText("WCDMA-66元A");
        listDate.clear();
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("itemTitle", "基本套餐费");
        map.put("itemText", "220.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "本地通话费");
        map.put("itemText", "120.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "增值业务费");
        map.put("itemText", "20.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "   3G上网费");
        map.put("itemText", "10.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "视频通话费");
        map.put("itemText", "10.00元");
        listDate.add(map);
        listAdapter.notifyDataSetChanged();
    }

    private void initSpinnerData() {
        calendar.setTime(new Date());
        for (int i = 1; i < 7; i++) {
            calendar.add(Calendar.MONTH, -1);
            String data = DateUtils.formatDateToYearAndMonth(calendar.getTime());
            selectData.add(data + "-账单明细");
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.query_history_back_btn:
                PageJumpUtils.jump(this, Home.class);
                break;
            case R.id.query_history_logout_btn:
                PageJumpUtils.logoutAndJumpToMainPage(this);
                break;
            case R.id.query_history_my_account_btn:
                PageJumpUtils.jumpToManageAccountPage(this);
                break;
            case R.id.query_history_fee_records_btn:
                PageJumpUtils.jumpToQueryPaymentPage(this);
                break;
            case R.id.query_history_exit:
                AlertDialogUtils.showYesNoDiaLog(this, "是否退出系统?", new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        SystemUtils.killSelf();
                        finish();
                    }
                });
                break;
        }

    }

    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Home.class);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }

    class SpinnerSelectedListener implements AdapterView.OnItemSelectedListener {
        private Handler mHandler = new Handler();

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                                   long arg3) {
            calendar.setTime(new Date());
            calendar.add(Calendar.MONTH, -1 - arg2);
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    String month = DateUtils.formatDateToYearAndMonth(calendar.getTime());
                    mFeeName.setText(month + "-历史账单");
                    queryHistoryList(calendar.getTime());
                }
            });
        }

        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }
}
